package com.example.administrator.szb.fragments.shoucang.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.ShouCangActivity;
import com.example.administrator.szb.bean.ShouCangBean;
import com.example.administrator.szb.fragments.shoucang.ItemsClickListener;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by stone on 2018/1/4.
 */

public class WDShouCangAdapter extends android.widget.BaseAdapter{
    private List<ShouCangBean.ReferBean> list;
    private Context context;
    LayoutInflater inflater;
    public WDShouCangAdapter(List list, Context context) {
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    private List selectList = new ArrayList();
    public void setSelectAll(){
        selectList.clear();
        for (int i=0;i<list.size();i++){
            selectList.add(list.get(i));
        }

        notifyDataSetChanged();
    }
    public void setSelectNo(){
        selectList.clear();
        notifyDataSetChanged();
    }

    public void showCheckBox(ViewHolder holder, final Object obj, View view, final int pos){
        view.setOnClickListener(null);
        holder.checkBox_ll.setVisibility(View.GONE);
        holder.my_check_box.setVisibility(View.GONE);

        if (ShouCangActivity.canShowCheckBox){
            holder.checkBox_ll.setVisibility(View.VISIBLE);
            if (selectList.contains(obj)){
                holder.my_check_box.setVisibility(View.VISIBLE);
            }
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(selectList.contains(obj)){
                        selectList.remove(obj);
                    }else {
                        selectList.add(obj);
                    }
                    notifyDataSetChanged();
                }
            });

        }else {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener!=null){
                        listener.onItemClick(pos);
                    }
                }
            });
        }
    }
    ItemsClickListener listener=null;
    public void setOnItemClick(ItemsClickListener l){
        listener=l;
    }

    public List getSelectList(){
        return selectList;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public ShouCangBean.ReferBean getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView = inflater.inflate(R.layout.item_shocang_wd,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.title.setText(getItem(position).getTitle());
        holder.sc.setText(getItem(position).getCollect_count()+"收藏");
        holder.huifu.setText(getItem(position).getReply()+"回复");
        holder.time.setText(getItem(position).getCreate_time());
        showCheckBox(holder,getItem(position), convertView,position);
        return convertView;
    }
    class ViewHolder{
        View my_check_box;
        LinearLayout checkBox_ll;
        TextView title,sc,huifu,time;
        public ViewHolder(View view){

            my_check_box = view.findViewById(R.id.my_check_box);
            checkBox_ll = view.findViewById(R.id.checkBox_ll);
            title = view.findViewById(R.id.title);
            sc = view.findViewById(R.id.huifu);
            huifu = view.findViewById(R.id.huifu);
            time = view.findViewById(R.id.time);
        }
    }
}
